<?php
# Sign up

function signup($username, $email){
	$c = connect();
	if(_validate($username, $email)){
		_register($username, $email);
		mysql_close($c);
		return true;
	}
	mysql_close($c);
	return false;
}

function _makePassword($length){
	$password = "";
	for($i = 0; $i<$length; $i++){
		$password .= chr(rand(32,126));
	}
	return $password;
}

function _register($username, $email){
	$pass = _makePassword(5);
	mysql_query("INSERT INTO newcommers (username, email, password, signup_date) 
		VALUES (\"$username\",\"$email\",\"$pass\",'".date_format(date_create(), 'Y-m-d H:i:s')."')"
	);
}

function _validate($username, $email){
	if($username == "" || $email == "")
		return false;

	# Test username and password are unique
	$r = mysql_query("SELECT username FROM users
		WHERE username = \"$username\" OR email = \"$email\"
		UNION
		SELECT username FROM newcommers
		WHERE username = \"$username\" OR email = \"$email\""
	);
	if(mysql_num_rows($r) > 0)
		return false;
	return true;
}

?>
